package com.shujia.source;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.connector.file.src.FileSource;
import org.apache.flink.connector.file.src.reader.StreamFormat;
import org.apache.flink.connector.file.src.reader.TextLineInputFormat;
import org.apache.flink.core.fs.Path;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.Arrays;

public class FileRead {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        FileSource<String> fs = FileSource.forRecordStreamFormat(new TextLineInputFormat() {
                }, new Path("spark/data/students.csv"))
                .build();
        DataStreamSource<String> src = env.fromSource(fs, WatermarkStrategy.noWatermarks(), "students");

        SingleOutputStreamOperator<String> words = src.flatMap((line, out) -> Arrays.asList(line.split(",")).forEach(out::collect),Types.STRING);

        SingleOutputStreamOperator<Tuple2<String, Integer>> res = words.map(word -> Tuple2.of(word, 1), Types.TUPLE(Types.STRING, Types.INT)).keyBy(tup -> tup.f0).sum(1);
        res.print();
        env.execute();
    }
}
